<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				fileKey:'52'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			formSubmit(){
				let _that = this
				_that.formdata.updateBottomFileFlag = true
				_that.formdata.bottomFileListStr = JSON.stringify(_that.$refs.autoform.formdata.bottomFileList || []) || ''
				if(!_that.edit){
					_that.om.formSave("lxmPrepaid/doAdd",_that.formdata,_that)
				}else{
					_that.om.formSave("lxmPrepaid/doUpdate",_that.formdata,_that)
				}
			},
			onSelectSalaryRepaymentMonth(){
				let _that = this
				if(_that.$refs.autoform.formdata.prepaidMoney){
					_that.joinStr()
				}
			},
			onSelectCashRepaymentDate(e){
				let _that = this
				let title1 = _that.$refs.autoform.getEditorTitle("title1","prepaidDate")
				let title2 = _that.$refs.autoform.getEditorTitle("title1","cashRepaymentDate")
				_that.$refs.autoform.formdata.cashRepaymentDate = e
				if(_that.$refs.autoform.formdata.prepaidDate){
					if(moment(e).isSameOrBefore(_that.$refs.autoform.formdata.prepaidDate)){
						errorModal(_that,'提示信息',title2+'不能小于申'+title1,()=>{
							_that.$refs.autoform.clear('title1',"cashRepaymentDate")
						})
					}
				}
				_that.joinStr()
			},
			onSelectPrepaidDate(date){
				let _that = this
				if(moment().subtract(1, 'days').isAfter(date)){
					errorModal(_that,'提示信息','无法选择当前日期之前的日期',()=>{
						_that.$refs.autoform.clear('title1',"prepaidDate")
					})
				}
			},
			onSelectRepaymentWay(item){
				let _that = this
				if(item.id == '6527'){
					_that.$refs.autoform.changeEditor("title1","salaryRepaymentMonth","type","selectbox")
					_that.$refs.autoform.changeEditor("title1","salaryRepaymentMoney","type","numberbox")
					_that.$refs.autoform.clear("title1","cashRepaymentDate")
					_that.$refs.autoform.changeEditor("title1","cashRepaymentDate","type","labelbox")
				}else{
					_that.$refs.autoform.clear("title1","salaryRepaymentMonth")
					_that.$refs.autoform.clear("title1","salaryRepaymentMoney")
					_that.$refs.autoform.changeEditor("title1","salaryRepaymentMonth","type","labelbox")
					_that.$refs.autoform.changeEditor("title1","salaryRepaymentMoney","type","labelbox")
					_that.$refs.autoform.changeEditor("title1","cashRepaymentDate","type","newdatebox")
				}
			},
			inputSalaryRepaymentMoney(val){
				let _that = this
				let title1 = _that.$refs.autoform.getEditorTitle('title1',"salaryRepaymentMoney")
				let title2 = _that.$refs.autoform.getEditorTitle('title1',"prepaidMoney")
				if(_that.$refs.autoform.formdata.prepaidMoney){
					if(Number(_that.$refs.autoform.formdata.prepaidMoney)<Number(val)){
						errorModal(_that,'提示信息',title1+'不能大于'+title2,()=>{
							_that.$refs.autoform.clear("title1","salaryRepaymentMoney")
						})
					}
				}else{
					errorModal(_that,'提示信息',title1+'不能大于'+title2,()=>{
						_that.$refs.autoform.clear("title1","salaryRepaymentMoney")
					})
				}
				if(_that.$refs.autoform.formdata.salaryRepaymentMonth){
					_that.joinStr()
				}else{
					_that.$refs.autoform.clear("title4","remark")
				}
			},
			joinStr(){
				let _that = this
				_that.om.request(
					"lxmPrepaid/joinStr",
					{
						userId: _that.userId,
						prepaidDate: _that.$refs.autoform.formdata.prepaidDate,
						prepaidMoney: _that.$refs.autoform.formdata.prepaidMoney,
						repaymentWayId: _that.$refs.autoform.formdata.repaymentWayId,
						cashRepaymentDate:_that.$refs.autoform.formdata.cashRepaymentDate,
						salaryRepaymentMonth: _that.$refs.autoform.formdata.salaryRepaymentMonth,
						salaryRepaymentMoney: _that.$refs.autoform.formdata.salaryRepaymentMoney
					}
				).then(res=>{
					if(res.value){
						_that.$refs.autoform.formdata.remark = res.value
					}
				})
			},
			initLoad(options) {
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url: "lxmPrepaid/getprepaidPerson",
						param: {
							userId: _that.userId
						}
					},
					urlUtil.dictZeroComboboxUrl('38492'),
					{
						url: "common/findYearMonthList",
						param: {
							after: 12
						}
					},
					{
						url: "lxmPrepaid/getaskleaveSta",
						param: {
							userId:_that.userId
						}
					}
				];
				if(fileId){
					ajaxArr.push({
						url:'lxmPrepaid/getValue',
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo", "zposId", "data", configSelectData(res[0], "id", "posName"), mainData)
					if(!_that.edit){
						changeEditor("title1", "prepaidPersonId", {
							defaultValue: _that.userId,
							defaultName: _that.userName
						}, null, mainData)
						changeEditor("title1", "prepaidPersonIDCard", {
							defaultName: res[1].value,
							defaultValue: res[1].value
						}, null, mainData)
					}
					changeEditor("title1","repaymentWayId","data",configSelectData(res[2],"id","dicname"),mainData)
					changeEditor("title1","salaryRepaymentMonth","data",configSelectData(res[3],"text","value"),mainData)
					
					_that.$nextTick(function() {
						_that.$refs.autoform.initSelectVal("topinfo", "zposId")
					})
					changeEditor("title3", "askleaveSta", {
						defaultName: res[4].value,
						defaultValue: res[4].value
					}, null, mainData)
					changeEditor("title1", "prepaidDate", {
						defaultName:moment().format('YYYY-MM-DD'),
						defaultValue: moment().format('YYYY-MM-DD')
					}, null, mainData)
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[5].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[5].value[index])
								_that.$set(_that.formdata,index,res[5].value[index])
							}
						}
						if(_that.$refs.autoform.formdata.repaymentWayId == '6527'){
							changeEditor("title1","salaryRepaymentMonth","type","selectbox",mainData)
							changeEditor("title1","salaryRepaymentMoney","type","numberbox",mainData)
							changeEditor("title1","cashRepaymentDate","type","labelbox",mainData)
						}else{
							changeEditor("title1","salaryRepaymentMonth","type","labelbox",mainData)
							changeEditor("title1","salaryRepaymentMoney","type","labelbox",mainData)
							changeEditor("title1","cashRepaymentDate","type","newdatebox",mainData)
						}
						_that.$nextTick(function(){
							_that.$refs.autoform.initSelectIndex("title1","repaymentWayId")
							_that.$refs.autoform.initSelectIndex("title1","salaryRepaymentMonth")
							
						})
					}
				}, options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {

		}
	}
</script>
<style></style>
